//
//  TextField.swift
//  iOSProjectExtensionSwift
//
//  Created by HBGG on 2020/9/4.
//  Copyright © 2020 lidianchao. All rights reserved.
//

import UIKit
import Kingfisher
extension UITextField {
    
    static func createBaseTextField(frame:CGRect,placeHold:String) ->UITextField {
        let textField = UITextField(frame: frame)
        textField.tintColor = ColorWithHex("#999999")
        textField.textColor = ColorWithHex("#0C2A59")
        let attrsDictionary = [NSAttributedString.Key.foregroundColor:ColorWithHex("#9EAABD")]
        let attrStr = NSAttributedString.init(string: placeHold, attributes: attrsDictionary)
        textField.font = UIFont.systemFont(ofSize: 14)
        textField.attributedPlaceholder = attrStr
        //设置内边距
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 8, height: 0))
        textField.leftView = leftView
        textField.leftViewMode = .always
        return textField
    }
    //构建图形验证码类型输入框
    static func createImageCodeTextField(frame:CGRect,placeHold:String,target:AnyObject,action:Selector) ->UITextField {
        let textField = createBaseTextField(frame: frame, placeHold: placeHold)
        let rightView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: 44))
        let rightLine = UILabel.createLineLabelWithFrame(frame: CGRect(x: 6, y: 10, width: kLineWitdth, height: 24))
        rightView.addSubview(rightLine)
        //验证码
        let imgBtn = UIButton(frame: CGRect(x: 20, y: 6, width: 90, height: 30))
        imgBtn.clipsToBounds = true
        imgBtn.kf.setBackgroundImage(with: URL(string: kGetImgCode_UrlRandom), for: .normal, placeholder: UIImage(named: "imageCodePlacehold"))
        imgBtn.layer.cornerRadius = 4
        imgBtn.addTarget(target, action: action, for: .touchUpInside)
        objc_setAssociatedObject(textField, "textfieldImageValue", imgBtn, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        rightView.addSubview(imgBtn)
        //此处用来设置rightView现实时机
        textField.rightView = rightView
        textField.rightViewMode = .always
        return textField
    }
    //构建发送验证码倒计时类型输入框
    static func createMsgCodeTextField(frame:CGRect,placeHold:String,target:AnyObject,action:Selector) ->UITextField {
        let textField = createBaseTextField(frame: frame, placeHold: placeHold)
        
        let rightView = UIView(frame: CGRect(x: 0, y: 0, width: 120, height: textField.viewHeight))
        let rightLine = UILabel.createLineLabelWithFrame(frame: CGRect(x: 0, y: 10, width: kLineWitdth, height: 24))
               rightView.addSubview(rightLine)
        rightView.addSubview(rightLine)
        let codeSendBtn = UIButton.createButtonWithTitle(title: "获取验证码", frame: CGRect(x:rightLine.maxX+1 , y: 0, width: rightView.viewWidth-1-rightLine.maxX, height: rightView.viewHeight), target: target, action: action, font: 14, textColor: kMainColor)
        codeSendBtn.titleLabel?.adjustsFontSizeToFitWidth = true
        rightView.addSubview(codeSendBtn)
        //此处用来设置rightView现实时机
        textField.rightView = rightView
        textField.rightViewMode = .always
        return textField
    }
    /*********************实例方法*******************/
    //添加圆角
    func addCorner() {
        self.layer.borderColor = ColorWithHex("E6E9EE").cgColor;
        self.layer.borderWidth = kLineWitdth;
        self.layer.cornerRadius = 4;
        self.layer.masksToBounds = true;
    }
    //添加底部横线
    func addBottomLine() {
        let line = UILabel.createLineLabelWithFrame(frame: CGRect(x: 0, y: self.viewHeight-kLineWitdth, width: self.viewWidth, height: kLineWitdth))
        line.backgroundColor = ColorWithHex("#f2f2f2")
        objc_setAssociatedObject(self, "bottomLine",line, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        self.addSubview(line)
        
    }
    //添加左侧标题
    func addLeftTitle(leftTitle:String) {
        let titleWidth = UILabel.getTextWidthWithString(text: leftTitle, font: 14)
        let view = UIView(frame: CGRect(x: 0, y: 0, width: titleWidth+leftDistance, height: self.viewHeight))
        let leftLabel = UILabel.createLabel(frame: CGRect(x: 5, y: 0, width: titleWidth, height: self.viewHeight), title: leftTitle, textColor: ColorWithHex("#54698A"), isCenter: false, font: 14, board: false)
        objc_setAssociatedObject(self, "textfieldImageValue",leftLabel, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        view.addSubview(leftLabel)
        self.leftView = view
        self.leftViewMode = .always
    }
    //添加左侧图片------用于密码输入框等有左侧小图标的输入框
    func addLeftImage(leftImage:UIImage,imageSize:CGSize) {
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: imageSize.width+leftDistance, height: self.viewHeight))
        
        let leftImageView = UIImageView(frame: CGRect(x: 0, y: (leftView.viewHeight-imageSize.height)/2, width: imageSize.width, height: imageSize.height))
        leftImageView.image = leftImage
        leftView.addSubview(leftImageView)
        self.leftView = leftView
        self.leftViewMode = .always
    }
}
